﻿using System;
using System.Web;
using System.Collections.Generic;
using System.Text;
using System.IO;


namespace ProjectBase.Utils
{
    public class CFile
    {
        #region Create Instance
        private static CFile instance = null;
        public static CFile Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new CFile();
                }
                return instance;
            }
        }
        #endregion

        //private string uploadPath = ConfigSite.UploadPath;// HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath + "/");
        public CFile()
        {
            //
            // TODO: Add constructor logic here
            //
        }
        public bool ExistsFile(string uploadPath, string strFileName)
        {
            if (uploadPath.Equals("")) uploadPath = ConfigSite.UploadPath;

            if (System.IO.File.Exists(uploadPath + strFileName))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public void DeleteFile(string uploadPath, string strFilename)
        {
            if (ExistsFile(uploadPath, strFilename))
            {
                FileInfo fi = new FileInfo(uploadPath + strFilename);
                fi.Delete();
            }
        }

        //private string UpLoad(HttpPostedFile pFile, string uploadDirName)
        //{
        //    //string strSavePath=strUploadPath+strFolderPath;
        //    if (pFile != null)
        //    {
        //        int iFileLen = pFile.ContentLength;
        //        if (iFileLen == 0)
        //        {
        //            return string.Empty;
        //        }
        //        this.CreateFolder(uploadDirName);
        //        string strEx = System.IO.Path.GetExtension(pFile.FileName);
        //        string strFilename = System.IO.Path.GetFileName(pFile.FileName);
        //        int iEx = 0;
        //        try
        //        {
        //            //while (System.IO.File.Exists(HttpContext.Current.Server.MapPath(strSavePath + strFilename)))
        //            while (System.IO.File.Exists(uploadPath + uploadDirName + "/" + strFilename))
        //            {
        //                iEx++;
        //                strFilename = System.IO.Path.GetFileNameWithoutExtension(pFile.FileName) + "_" + iEx.ToString() + strEx;
        //            }
        //            //pFile.SaveAs(HttpContext.Current.Server.MapPath(strSavePath + strFilename));
        //            pFile.SaveAs(uploadPath + uploadDirName + "/" + strFilename);
        //            //thực hiện xong thêm file
        //            return strFilename;
        //        }
        //        catch (Exception ex)
        //        {
        //            //new ErrorLog().WriteError(ex.Message);
        //            return string.Empty;
        //        }
        //    }
        //    else
        //    {
        //        return string.Empty;
        //    }
        //}
        //private void DeleteFile(string uploadDirName, string strFilename)
        //{
        //    //Xoa file ra khoi he thong				
        //    //if (System.IO.File.Exists(HttpContext.Current.Server.MapPath(strSavePath + strPathFile)))
        //    if (System.IO.File.Exists(uploadPath + uploadDirName + "/" + strFilename))
        //    {
        //        //System.IO.FileInfo fi =new System.IO.FileInfo(HttpContext.Current.Server.MapPath(strSavePath + strPathFile));
        //        System.IO.FileInfo fi = new System.IO.FileInfo(uploadPath + uploadDirName + "/" + strFilename);
        //        fi.Delete();
        //    }
        //}
        //private string returnImage(string src, string filename, string comment, string width, string height)
        //{
        //    if (src.Length > 0)
        //    {
        //        string imgPhys = HttpContext.Current.Server.MapPath(src + filename);
        //        //string imgPhys	= HttpContext.Current.Server.MapPath(filename);
        //        string imgHttp = src + filename;


        //        if (File.Exists(imgPhys))
        //        {
        //            if (height == "")
        //                return "<img src=\"" + imgHttp + "\" border=0 title='" + comment + "' width=" + width + " align=left>";
        //            else
        //                return "<img src=\"" + imgHttp + "\" border=0 title='" + comment + "' width=" + width + " height=" + height + " align=left>";
        //        }
        //        else
        //            return "";
        //    }
        //    else
        //        return "";
        //}

        
        //private bool ExistsFile(string strPathFile)
        //{

        //    string p = ConfigSite.FileUpload + "/" + strPathFile;
        //    if (System.IO.File.Exists(p))
        //        return true;
        //    else
        //        return false;
        //}
        //private string ImageExistsFile(string strPathFile, string baseUrl)
        //{
        //    if (ExistsFile(strPathFile))
        //        return baseUrl + strPathFile;
        //    else
        //        return baseUrl + "Images/SP_NoAvailable.jpg";
        //}
        //private string GetImage(string image, string dirName)
        //{
        //    if (ExistsFile("", image))
        //    {
        //        return image;
        //    }
        //    else
        //    {
        //        return "Images/NoImageAvailable.gif";
        //    }
        //}
        //private string GetImageWithAvailable(string image, string imgAvailable)
        //{
        //    if (ExistsFile("", image))
        //    {
        //        return image;
        //    }
        //    else
        //    {
        //        return "Images/" + imgAvailable;
        //    }
        //}
        //private string VirtualPathFull(string path)
        //{
        //    if (path.Length > 0)
        //    {
        //        string virtualPath = HttpContext.Current.Request.ApplicationPath;
        //        if (virtualPath.Length > 1)
        //            return virtualPath + "/" + path;
        //        else
        //            return path;
        //    }
        //    else
        //    {
        //        return "";
        //    }
        //}
        //private string GetFileExtension(string strPathFile)
        //{
        //    if (ExistsFile(strPathFile)){
        //        string strPath = ConfigSite.FileUpload + "/" + strPathFile;
        //        FileInfo file = new FileInfo(strPath);
        //        return file.Extension;           
        //    }
        //    return "";            
        //}
        //private int[] ResizeImage(int width, int height, int widthNew)
        //{
        //    int[] arrSize = new int[2];
        //    double scale;
        //    scale = (double)widthNew / width;
        //    arrSize[0] = (int)(scale * width);
        //    arrSize[1] = (int)(scale * height);
        //    return arrSize;
        //}

        //private int[] GetSizeWH(string strPathFile)
        //{
        //    int[] arrSize = new int[2];
        //    if (ExistsFile(strPathFile))
        //    {

        //        System.Drawing.Image objImage = System.Drawing.Image.FromFile(strPathFile);
        //        arrSize[0] = objImage.Width;
        //        arrSize[1] = objImage.Height;
        //    }
        //    return arrSize;
        //}

        //private void CreateFolder(string uploadDirName)
        //{
        //    //Xoa file ra khoi he thong				
        //    //if (System.IO.File.Exists(HttpContext.Current.Server.MapPath(strSavePath + strPathFile)))
        //    if (!System.IO.Directory.Exists(uploadPath + uploadDirName))
        //    {
        //        //System.IO.FileInfo fi =new System.IO.FileInfo(HttpContext.Current.Server.MapPath(strSavePath + strPathFile));
        //        System.IO.Directory.CreateDirectory(uploadPath + uploadDirName);
        //    }
        //}
    }
}

